package service

import (
	"time"

	"github.com/shirou/gopsutil/v3/cpu"
	"github.com/shirou/gopsutil/v3/disk"
	"github.com/shirou/gopsutil/v3/mem"

	"publish-manage/internal/ws"
)

var ServerInfoService = new(serverInfoService)

type serverInfoService struct {
}

func (_ *serverInfoService) GetMemoryInfo() *ws.MemoryInfo {
	v, _ := mem.VirtualMemory()
	return &ws.MemoryInfo{
		Total:       v.Total,
		Available:   v.Available,
		Used:        v.Used,
		UsedPercent: v.UsedPercent,
	}
}

func (_ *serverInfoService) GetCpuPercent(d time.Duration) float64 {
	c, _ := cpu.Percent(d, false)
	return c[0]
}
func (_ *serverInfoService) GetDiskUsage() *ws.DiskUsage {
	d, _ := disk.Usage("/")
	return &ws.DiskUsage{
		Total:       d.Total,
		Free:        d.Free,
		Used:        d.Used,
		UsedPercent: d.UsedPercent,
	}
}
